package com.ruoyi.system.controller;

import java.util.List;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.Bo.UsersBo;
import com.ruoyi.system.utils.BizCodeEnum;
import com.ruoyi.system.utils.validate.AddGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Users;
import com.ruoyi.system.service.IUsersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户管理Controller
 * 
 * @author ruoyi
 * @date 2022-08-18
 */
@RestController
@RequestMapping("/system/users")
public class UsersController extends BaseController
{
    @Autowired
    private IUsersService usersService;

    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:users:list')")
    @GetMapping("/list")
    public TableDataInfo list(Users users)
    {
        startPage();
        List<Users> list = usersService.selectUsersList(users);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:users:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Users users)
    {
        List<Users> list = usersService.selectUsersList(users);
        ExcelUtil<Users> util = new ExcelUtil<Users>(Users.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:users:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Integer userId)
    {
        return AjaxResult.success(usersService.selectUsersByUserId(userId));
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('system:users:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody UsersBo bo)
    {
        //判断密码和确认密码是否为空
        if ((bo.getUserPass()==null&&bo.getUserPass().equals(""))
                || (bo.getConfirmPass()==null&&bo.getConfirmPass().equals(""))) {
            return AjaxResult.error(BizCodeEnum.PASSWORD_ISEMPTY_EXCEPTION.getCode()
                    , BizCodeEnum.PASSWORD_ISEMPTY_EXCEPTION.getMsg());
        }
        //判断密码和确认密码是否相等
        if (!bo.getUserPass().equals(bo.getConfirmPass())){
            return AjaxResult.error(BizCodeEnum.PASSWORD_NOT_SAME_EXCEPTION.getCode()
                    ,BizCodeEnum.PASSWORD_NOT_SAME_EXCEPTION.getMsg());
        }

        //用于判断邮箱是否合法
        //设置一个正则表达式,[\w]匹配包括下划线的任何单词字符，等价于[A-Za-z0-9_]
        String reg = "[\\w]+@[\\w]+.[\\w]+[\\w]";
        if (bo.getUserEmail()==null || !bo.getUserEmail().matches(reg)) {
            return AjaxResult.error(BizCodeEnum.ILLEGAL_EMAIL_EXCEPTION.getCode()
                    , BizCodeEnum.ILLEGAL_EMAIL_EXCEPTION.getMsg());
        }

        //用于手机号验证
        if (bo.getUserPhone()==null
                || !Pattern.matches("^1[3-9]\\d{9}$", bo.getUserPhone())) {
            return AjaxResult.error(BizCodeEnum.ILLEGAL_PHONE_EXCEPTION.getCode()
                    ,BizCodeEnum.ILLEGAL_PHONE_EXCEPTION.getMsg());
        }
        return toAjax(usersService.insertUsers(bo));
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('system:users:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Users users)
    {
        return toAjax(usersService.updateUsers(users));
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('system:users:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Integer[] userIds)
    {
        return toAjax(usersService.deleteUsersByUserIds(userIds));
    }
}
